package com.tanhua.controller;


import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.UserInfoVO;
import com.tanhua.service.UserInfoService;
import com.tanhua.service.UserService;
import com.tanhua.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserService userService;

    /**
     * 回显用户资料
     * @param token
     * @return
     */
    @GetMapping
    public ResponseEntity findUserInfo(@RequestHeader("Authorization") String token){
        //User user = userInfoService.findUserByToken(token);
        User user = UserHolder.getUser();

        return userInfoService.findUserInfo(user.getId());
    }

    /**
     * 更新用户资料
     * @param userInfo
     * @param token
     * @return
     */
    @PutMapping
    public ResponseEntity updateInfo(@RequestBody UserInfo userInfo,
                                     @RequestHeader("Authorization") String token){
        //User user = userInfoService.findUserByToken(token);
        User user = UserHolder.getUser();
        userInfo.setId(user.getId());
        return userInfoService.updateInfo(userInfo);

    }

    /**
     * 更新头像
     * @param headPhoto
     * @param token
     * @return
     * @throws IOException
     */
    @PostMapping("/header")
    public ResponseEntity updateHeader(MultipartFile headPhoto,
                                       @RequestHeader("Authorization") String token) throws IOException {
       // User user = userInfoService.findUserByToken(token);
        User user = UserHolder.getUser();
        return userInfoService.saveHeader(headPhoto,user.getId());
    }

    /**
     * GET/users/settings
     * 读取用户设置信息
     * @return
     */
    @GetMapping("/settings")
    public ResponseEntity findSettings(){
        return userInfoService.findSettings();
    }
    /**
     * POST/users/questions
     * 修改陌生人问题
     */
    @PostMapping("/questions")
    public ResponseEntity updateQuestions(@RequestBody Map<String,String> map){
        String content = map.get("content");
        return userInfoService.updateQuestions(content);
    }
    /**
     * POST/users/notifications/setting
     * 修改通知设置
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity updateSetting(@RequestBody Settings settings){
        return userInfoService.updateSetting(settings);
    }
    /**
     * GET/users/blacklist
     * 黑名单列表
     */
    @GetMapping("/blacklist")
    public ResponseEntity blackList(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){
        return userInfoService.blackList(page,pagesize);
    }

    /**
     * DELETE/users/blacklist/:uid
     * 黑名单移除
     */
    @DeleteMapping("/blacklist/{userId}")
    public ResponseEntity deleteBlackList(@PathVariable("userId") Integer blackUserId){
        return userInfoService.deleteBlackList(blackUserId);
    }

    /**
     * POST/users/phone/sendVerificationCode
     * 发送短信验证码
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        User user = UserHolder.getUser();
        return userService.sendSms(user.getMobile());
    }
    /**
     * POST/users/phone/checkVerificationCode
     * 校验验证码
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map<String,String> map){
        String code = map.get("verificationCode");
        System.out.println(code);
        return userService.checkVerificationCode(code);
    }
    /**
     * POST/users/phone
     * 保存修改的手机号
     */
    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestBody Map<String,String> map,
                                      @RequestHeader("Authorization") String token){
        System.out.println(token);
        String phone = map.get("phone");
        return userService.updatePhone(phone,token);

    }
    /**
     * GET/users/counts
     * 统计喜欢,互相喜欢,粉丝数量
     */
    @GetMapping("/counts")
    public ResponseEntity counts(){
        return userService.counts();
    }
    /**
     * GET/users/friends/:type
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity typeList(@PathVariable("type")Integer type,Integer page,Integer pagesize,String nickname){
        return userService.typeList(type,page,pagesize);

    }

    /**
     * POST/users/fans/:uid
     * 粉丝-喜欢
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity fans(@PathVariable("uid")Long fanId){
        return userService.fansLike(fanId);
    }
}
